﻿using System.Configuration;
using System.Linq;

using MSAcademic.v2.Database;
using MSAcademic.v2.Infrastructure.Utility;

namespace MSAcademic.v2.Infrastructure.User.Impl
{
    public class DatabaseUserCommands : IUserCommands
    {
        public void SaveNew(string username, string password, string email, string regToken)
        {
            var tokenPripravnik = ConfigurationManager.AppSettings["TokenPripravnik"];
            var tokenMsp = ConfigurationManager.AppSettings["TokenMsp"];
            var tokenVanjskiSuradnik = ConfigurationManager.AppSettings["TokenVanjskiSuradnik"];

            using (var db = new MSAcademicDatabaseContainer())
            {
                var user = new Database.User
                {
                    Username = username,
                    Password = PasswordHashService.CreatePasswordHash(password),
                    Email = email,
                    FirstName = "Nepoznati",
                    LastName = "Netko",
                    Description = "Reci nam par riječi o sebi!",
                    IsHidden = false,
                    FacebookLink = string.Empty,
                    TwitterLink = string.Empty,
                    OtherLink = string.Empty,
                    LinkedInLink = string.Empty,
                    ProfilePictureUrl = string.Empty,
                    UserRightsCsv = ""
                };

                if (regToken == tokenPripravnik)
                {
                    user.ActingRole     = (int)ActingRole.Pripravnik;
                    user.UserRightsCsv  = "2;3";
                }
                else if (regToken == tokenMsp)
                {
                    user.ActingRole = (int)ActingRole.Msp;
                    user.UserRightsCsv = "2;3";
                }
                else if (regToken == tokenVanjskiSuradnik)
                {
                    user.ActingRole = (int)ActingRole.VanjskiSuradnik;
                    user.UserRightsCsv = "1;2;3;4;7";
                }

                db.Users.Add(user);
                db.SaveChanges();
            }
        }

        public void ChangePassword(int id, string newPassword)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var user = db.Users.SingleOrDefault(x => x.Id == id);
                if (user == null)
                {
                    return;
                }
                user.Password = PasswordHashService.CreatePasswordHash(newPassword);
                db.SaveChanges();
            }
        }
        public void UpdateProfile(int id,string userName,string password,string firstName,string lastName,string desc,string email,string facebook,string twitter,string linkedIn,string otherLink)
        {
            using(var db= new MSAcademicDatabaseContainer())
            {
                var user = db.Users.SingleOrDefault(m => m.Id == id);
                if (user == null) return;

                user.Username = userName;
                user.FirstName = firstName;
                user.LastName = lastName;
                user.Description = desc;
                user.Email = email;
                user.FacebookLink = facebook ?? "";
                user.TwitterLink = twitter ?? "";
                user.OtherLink = otherLink ?? "";
                user.LinkedInLink = linkedIn ?? "";
                if(!string.IsNullOrWhiteSpace(password))
                {
                    user.Password = PasswordHashService.CreatePasswordHash(password);
                }
                db.SaveChanges();

            }
        }
    }
}
